//
//  MoreJokesViewController.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/16.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "MoreJokesViewController.h"
#import "UMSocial.h"
@interface MoreJokesViewController ()<UMSocialUIDelegate>
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *JokesArray;
@property (nonatomic,retain) MBProgressHUD * hud;
@end

@implementation MoreJokesViewController


- (void)reloadMoreJokes {
   
    NSString *url = [NSString stringWithFormat:kMoreJokes,self.page];
    [LORequestManger GET:url success:^(id response) {
        NSDictionary  *dic = (NSDictionary *)response;
        NSDictionary *Dic = dic[@"result"];
        for (NSDictionary *pic in Dic[@"data"]) {
            LatestJokeModel  *JokesModel = [LatestJokeModel shareJsonWithDictionary:pic];
            NSLog(@"________%@", JokesModel);
            
            
            [self.JokesArray addObject:JokesModel];
            
            
        }
        
         [_hud hide:YES];
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        NSLog(@"%@",error);
        
    }];
    
    
    
}
- (NSMutableArray *)JokesArray {
    if (!_JokesArray) {
        _JokesArray = [[NSMutableArray alloc] init];
    }
    
    return _JokesArray;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    BlurImageView *blurimageView = [[BlurImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
       self.tableView.backgroundView = blurimageView;
    self.tableView.separatorStyle =  0;
    [self.tableView registerClass:[JokeTableCell class] forCellReuseIdentifier:@"jokeCell"];
    self.page = 2;
    [self p_setupProgressHud];
    
    [self reloadMoreJokes];
    
    [self setUIBarButtonItem];
    [self reloadFunctions];
   

    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

//菊花

- (void)p_setupProgressHud
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.minSize = CGSizeMake(50, 50);
    _hud.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_hud];
    
    [_hud show:YES];
}

//下拉刷新
- (void)reloadFunctions {

    __block MoreJokesViewController *root = self;
    [self.tableView addFooterWithCallback:^{
        NSLog(@"111");
        root.page ++;
        [root reloadMoreJokes];
        [root.tableView reloadData];
        [root.tableView footerEndRefreshing];
    }];
    
    [self.tableView addHeaderWithCallback:^{
        NSLog(@"111");
       
        [root.tableView reloadData];
        [root.tableView headerEndRefreshing];
    }];
    



}
- (void)setUIBarButtonItem {
    
    UIBarButtonItem * right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:( UIBarButtonSystemItemFastForward) target:self action:@selector(clickRight:)];
    self.navigationItem.rightBarButtonItem = right;
}

- (void)clickRight:(UIBarButtonItem *)button {
    
   
    [self transitionWithType:@"suckEffect" WithSubtype:kCATransitionFromLeft ];
    
    

    [self dismissViewControllerAnimated:NO completion:nil];
}
#pragma CATransition动画实现
- (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype
{
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    
    //设置运动时间
    animation.duration = 1.0f;
    
    //设置运动type
    animation.type = type;
    if (subtype != nil) {
        
        //设置子类
        animation.subtype = subtype;
    }
    
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    
    [self.view.layer addAnimation:animation forKey:@"animation"];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    return self.JokesArray.count;;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JokeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jokeCell" forIndexPath:indexPath];

    NSArray * arr = @[@"开心一刻送给珍贵的你࿐",@"笑死你不偿命࿐",@"你有这些智慧么࿐",@"放松一些,体会精彩人生࿐",@"更多精彩尽在笑笑天气࿐"];
    int a = arc4random()%5;
   // cell.showLabelOne.title.text = arr[indexPath.row];
    // Configure the cell...
    
    
    LatestJokeModel  *JokesModel = self.JokesArray[indexPath.row];
    if (JokesModel.content.length == 0) {
        cell.JokesIntroduce.text = @"该条数据消失了...肿么办";
        //        cell.JokesIntroduce.font = [UIFont boldSystemFontOfSize:20];
        //        cell.JokesIntroduce.textAlignment = 1;
        //cell.backgroundColor = [UIColor cyanColor];
    }
    else {
        cell.JokesIntroduce.text = JokesModel.content;}
        [cell.showLabelTwo addTarget:self action:@selector(clickButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.shareButton addTarget:self action:@selector(sharejoke:) forControlEvents:(UIControlEventTouchUpInside)];
    cell.shareButton.tag = 2000 + indexPath.row;

    cell.showLabelTwo.tag = 1000 + indexPath.row;
    
    cell.showLabelOne.title.text = arr[a];
    cell.showLabelTwo.selected = NO;
    NSMutableArray *modelArray = [[PlayerDataBase shareDataBase] selegetAllFromJokeTable];
    
    for (LatestJokeModel *model in modelArray) {
        if ([JokesModel.content isEqualToString:model.content]) {
            cell.showLabelTwo.selected = YES;
        }
        
    }
    return cell;
}

- (void)sharejoke:(UIButton *)button {
    
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
    
    if (name!=nil) {
        
        LatestJokeModel *model = self.JokesArray[button.tag - 2000];
        
        [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5604fd14e0f55abb670003f8" shareText:model.content shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToDouban,UMShareToRenren,UMShareToSina,UMShareToTencent, nil] delegate:self];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alert show];
        [[weatherHandle shareWetherHandle]p_removeAlertView:alert];
        [[weatherHandle shareWetherHandle] pleasedLoded:self];
        
    }

    
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kWidth/2;
}

- (void)clickButton:(UIButton *)button {
    button.selected = !button.selected;
    if (button.selected == YES) {
        //显示alertView提示用户
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        //0.3秒后alertView消失
        [self performSelector:@selector(p_removeAlertView:) withObject:alertView afterDelay:0.3];
  }
    
    [[PlayerDataBase shareDataBase]createTables];
    [[PlayerDataBase shareDataBase]insertJokeModelToJokeTable:self.JokesArray[button.tag - 1000]];
}

- (void)p_removeAlertView:(UIAlertView *)alertView
{
    //alertView消失
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
